<?php
namespace app\index\controller;

use app\common\model\CompanyHuanyuan as CompanyHuanyuanModel;

/**
 * 寺院还愿控制器
 * Class Companyhuyuan
 * @package app\index\controller
 */
class Companyhuanyuan extends Base
{
    /**
     * [index  规格]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $user = $this->user;
        $model = new CompanyHuanyuanModel;
        $gongdeng = config('huanyuan');
        try {
            $company = $model->findData($user['comid'], 'comid', 'foxiang,gonghua,gongguo');
            if(!empty($company)){
                $company['foxiang'] = unserialize($company['foxiang']);
                $company['gonghua'] = unserialize($company['gonghua']);
                $company['gongguo'] = unserialize($company['gongguo']);
            }else{
                $company = $gongdeng;
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'], $user['comid'], '查看寺院还愿规格');
        return admin_json(1, '获取成功', $company);
    }
    /**
     * [edit  编辑]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function edit()
    {
        $model = new CompanyHuanyuanModel;
        $param = input('param.');
        $user = $this->user;
        try{
            $gongdeng = $model->findData($user['comid'],'comid');
            $param['foxiang'] = serialize($param['foxiang']);
            $param['gonghua'] = serialize($param['gonghua']);
            $param['gongguo'] = serialize($param['gongguo']);
            if(empty($gongdeng)){
                $param['comid'] = $user['comid'];
                $model->addData($param);
            }else{
                $model->editDataBy($param,['comid'=>$user['comid']]);
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'], $user['comid'], '编辑寺院还愿规格');
        return admin_json(1, '编辑成功');
    }
    /**
     * [restore  还原默认]
     * @param
     * @return mixed|\think\response\Json
     * @author [C。]
     */
    public function restore()
    {
        $user = $this->user;
        try{
            $gongdeng = config('huanyuan');
        }catch (\Exception $e) {
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        writelog($user['mid'], $user['comid'], '还原默认还愿配置');
        return admin_json(1, '获取成功',$gongdeng);
    }
}